#region Copyright (c) 2003, newtelligence AG. All rights reserved.

/*
// Copyright (c) 2003, newtelligence AG. (http://www.newtelligence.com)
// Original BlogX Source Code: Copyright (c) 2003, Chris Anderson (http://simplegeek.com)
// All rights reserved.
//  
// Redistribution and use in source and binary forms, with or without modification, are permitted 
// provided that the following conditions are met: 
//  
// (1) Redistributions of source code must retain the above copyright notice, this list of 
// conditions and the following disclaimer. 
// (2) Redistributions in binary form must reproduce the above copyright notice, this list of 
// conditions and the following disclaimer in the documentation and/or other materials 
// provided with the distribution. 
// (3) Neither the name of the newtelligence AG nor the names of its contributors may be used 
// to endorse or promote products derived from this software without specific prior 
// written permission.
//      
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS 
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
// AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 
// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 
// OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// -------------------------------------------------------------------------
//
// Original BlogX source code (c) 2003 by Chris Anderson (http://simplegeek.com)
// 
// newtelligence is a registered trademark of newtelligence Aktiengesellschaft.
// 
// For portions of this software, the some additional copyright notices may apply 
// which can either be found in the license.txt file included in the source distribution
// or following this notice. 
//
*/

#endregion

using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace newtelligence.DasBlog.Web.Core.Macros
{
    public class NewtelligenceProperties
    {
        protected SharedBasePage requestPage;

        public NewtelligenceProperties(SharedBasePage page)
        {
            requestPage = page;
        }


        public virtual Control AdminBar()
        {
            if (!requestPage.HideAdminTools && SiteSecurity.IsValidContributor())
            {
                return requestPage.LoadControl("AdminNavBar.ascx");
            }
            else
            {
                return new LiteralControl("");
            }
        }

        /// <summary>
        /// Renders a control only if the user is logged in and is an administrator.
        /// Associated with the macro &lt;%ASPNETAdminControl(...)%&gt;
        /// </summary>
        /// <param name="controlName">String with relative path to ASCX control</param>
        public virtual Control ASPNETAdminControl(string controlName)
        {
            if (!requestPage.HideAdminTools && requestPage.Request.IsAuthenticated && SiteSecurity.IsInRole("admin"))
            {
                return requestPage.LoadControl(controlName);
            }
            else
            {
                return new LiteralControl("");
            }
        }

        public virtual Control ASPNETControl(string controlName)
        {
            return requestPage.LoadControl(controlName);
        }

        /// <summary>
        /// Renders a control only if the user is logged in
        /// Associated with the macro &lt;%ASPNETLoggedInControl(...)%&gt;
        /// </summary>
        /// <param name="controlName">String with relative path to ASCX control</param>
        public virtual Control ASPNETLoggedInControl(string controlName)
        {
            if (!requestPage.HideAdminTools && requestPage.Request.IsAuthenticated)
            {
                return requestPage.LoadControl(controlName);
            }
            else
            {
                return new LiteralControl("");
            }
        }

        public virtual Control Disclaimer(string disclaimerPath)
        {
            if (disclaimerPath == null)
            {
                disclaimerPath = "SiteConfig/disclaimer.format.html";
            }

            string fullpath = SiteUtilities.MapPath(disclaimerPath);
            string value;
            using (StreamReader reader = new StreamReader(fullpath))
            {
                value = reader.ReadToEnd();
            }

            return new LiteralControl(value);
        }

        public virtual Control DrawArchiveMonths()
        {
            return new ArchiveMonthsList();
        }

        public virtual Control DrawCategories()
        {
            return InternalDrawCategories(true);
        }

        public virtual Control DrawCategoriesWithoutFeed()
        {
            return InternalDrawCategories(false);
        }

        public virtual Control DrawMainCategories()
        {
            return InternalDrawCategories(true);
        }

        public virtual Control DrawSubCategories()
        {
            return new LiteralControl("");
        }

        private Control InternalDrawCategories(bool feed)
        {
            CategoryList control = new CategoryList(feed);
            control.Categories = requestPage.DataService.GetCategories();
            return control;
        }

        public virtual Control LoginBox()
        {
            if (!requestPage.HideAdminTools && requestPage.Request.IsAuthenticated &&
                !requestPage.Request.Path.ToLower().EndsWith("login.aspx"))
            {
                return requestPage.LoadControl("Logout.ascx");
            }
            else
            {
                HyperLink hl = new HyperLink();
                hl.Text = SharedBasePage.CoreStringTables.GetString("text_log_on");
                hl.NavigateUrl = "Login.aspx";

                return hl;
            }
        }

        public virtual Control LogoutBox()
        {
            if (!requestPage.HideAdminTools && requestPage.Request.IsAuthenticated)
            {
                return requestPage.LoadControl("Logout.ascx");
            }
            else
            {
                return new LiteralControl("");
            }
        }

        public virtual Control Search()
        {
            return requestPage.LoadControl("Search.ascx");
        }
    }
}